import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    will() {
    }
    @o exhibitionList = [];
    @o startTime = '';
    @o endTime = '';
    @o keyWord = '';
    @o recordStartTime = '';
    @o recordEndTime  = '';
    @o recordKeyword = '';
    @o total = 0;
    @o currentPage = 1;
    @o firstRequest = false;

    @a
    dataTypeChange(v,item){
        let mIndex = this.exhibitionList.findIndex((i) => {
            return item.id === i.id
        });
        if(this.exhibitionList[mIndex].addViewLevel === v.value){
            return;
        }
        this.exhibitionList[mIndex].addViewLevel = v.value;
        request({
            url:$$.api('getViewLevelAdd'),
            method:'post',
            data:{
                id:item.id,
                addViewLevel:v.value,
            }
        }).then(res => {
            this.stores.Message.showMessage(1,'修改成功');
        }).catch(err => {
            this.stores.Message.showMessage(2,'修改失败');
        })
    }

    @a
    paginationChange(current) {
        this.currentPage = current;
        this.getExhibitionList(true);
    }


    getExhibitionList(loading){
        loading && this.stores.Frame.showLoading();
        request({
            url:$$.api('getExhibitionList'),
            method:'GET',
            data:{
                pageNumber: this.currentPage,
                pageSize: $$.SEM.EXHIBITION_LIST_SIZE,
                startTime: this.recordStartTime  || null,
                endTime: this.recordEndTime || null,
                key: this.recordKeyword || null,
            }
        }).then(res => {
            this.firstRequest = true;
            this.stores.Frame.hideLoading();
            this.exhibitionList = res.data.artShowList;
            this.total = res.data.total;
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }

    @a
    startTimeChange(e) {
        this.startTime = e._d && e._d.getTime();
    }

    @a
    endTimeChange(e) {
        this.endTime = e._d && e._d.getTime();
    }

    @a
    clearStartTime() {
        this.startTime = '';
    }

    @a
    clearEndTime() {
        this.endTime = '';
    }

    @a
    exhibitionNameChange(v) {
        this.keyWord = v;
    }

    @a
    closeHandle() {
        this.keyWord = '';
        this.search();
    }

    @a
    nameInput(e) {
        if (e.which === 13) {
            this.search();
        }
    }

    @a
    search() {
        if (!this.startTime && this.endTime) {
            this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '开始时间不能为空'});
            return;
        }
        if (this.startTime && !this.endTime) {
            this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '结束时间不能为空'});
            return;
        }
        if (this.startTime > this.endTime) {
            this.stores.Message.fire('message', {
                type: $$.SEM.WARNING_TEXT,
                text: '开始时间不能大于结束时间'
            });
            return;
        }
        this.currentPage = 1;
        // if (this.startTime && this.endTime && this.startTime === this.endTime) {
        //     this.stores.Message.fire('message', {
        //         type: $$.SEM.WARNING_TEXT,
        //         text: '开始时间不能和结束时间相同'
        //     });
        //     return;
        // }
        this.recordStartTime= this.startTime;
        this.recordEndTime  = this.endTime;
        this.recordKeyword = this.keyWord;
        this.getExhibitionList(true)
    }

    searchTime(){
        if (!this.startTime && !this.endTime) {
            this.search()
        }
        this.search()
    }



    cancelExhibitionRelease(id){
        request({
            url:$$.api('cancelExhibitionRelease'),
            method: 'get',
            data:{
                id:id,
            }
        }).then(res => {
            this.stores.Message.fire('message', {
                type: $$.SEM.SUCCESS_TEXT,
                text: res.message
            });
            this.getExhibitionList();
        }).catch(err =>{
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }

    urlClick(data){
        let prefix = global.window.location.origin.indexOf('manage-test') > -1 ? 'https://staging.artivr.com' : 'https://www.artivr.com';
        global.window.open(`${prefix}/exhibition/${data.id}/detail?social=1`);

    }

    sortClick(data){
        this.stores.SortDialog.fire('sortDialog:show', {
            sort: data.sort,
            confirmFunc: (sort) => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('sortExhibition'),
                    method: 'post',
                    data: {
                        sort: sort,
                        id: data.id
                    }
                }).then(res => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(1, res.message);
                    this.getExhibitionList();
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                })
            }
        })
    }

    cancelRelease(data){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            title:'取消发布',
            tips:'取消发布后，画展将变为未发布',
            showIcon: true,
            hideCancel:true,
            func: () => {
                this.cancelExhibitionRelease(data.id)
            }
        });
    }
    getExhibitionExcel(){
        request({
            url:$$.api('downloadExhibition'),
            method:'get',
            data:{
                startTime:this.recordStartTime || null,
                endTime:this.recordEndTime || null,
                key: this.recordKeyword || null,
            }
        }).then(res => {
            this.stores.Message.showMessage(1, '导出成功');
            this.downUrl = res.data.url;
            let a = document.createElement('a');
            a.href = 'http://' + this.downUrl;
            a.download = '画展数据';
            a.click();
        }).catch(err =>{
            this.stores.Message.showMessage(2, err.message);
        })
    }
    @a
    downloadExcel(){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips:'确定导出当前所选画展数据吗？',
            func: () => {
                this.getExhibitionExcel()
            }
        });
    }



    mount() {
        this.getExhibitionList();
    }

}

export default Store;